
import {
  queryPromotionPeopleRank
} from "../../../../service/jb_promotion_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    topSwiperIndex: 0,
    requestData: {
      page: 1,
      limit: 50,
      type: "week",
    },
    dataList: [

    ],
    rankTopThreeArr: [

    ],
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
    refreshFlag: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryPromotionPeopleRank('refresh');

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  queryPromotionPeopleRank(type) {
    wx.showLoading();

    queryPromotionPeopleRank(this.data.requestData.page, this.data.requestData.limit, this.data.requestData.type).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        // let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        let arr = res.data || []
        this.setData({
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: true,
        })
        if (arr.length < 3) {
          this.setData({
            rankTopThreeArr: arr || [],
            dataList: [],
          })
        } else {
          this.setData({
            rankTopThreeArr: arr.slice(0, 3),
            dataList: arr.slice(3)
          })
        }
      }else{
        this.setData({
          rankTopThreeArr: [],
          dataList: [],
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.requestData.page != 1) {
        this.setData({
          'requestData.page': this.data.requestData.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error('JJLog:queryPromotionPeopleRank', err);
    })
  },
  swiperAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topSwiperIndex: index,
      'requestData.type': index == 0 ? 'week' : 'month'
    })
    this.queryPromotionPeopleRank();
  },

  //下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      'requestData.page': 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryPromotionPeopleRank('refresh');
  },
  handleLoadmore() {
    return;
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      'requestData.page': this.data.requestData.page + 1
    })
    this.queryPromotionPeopleRank()
  },

})